extends BattleState
@onready var card_table = $"../../CardTable"
@onready var battle_state_machine = $".."

func _ready():
	card_table.card_select.card_table_card_select_window_cancel.connect(_on_card_table_card_select_window_cancel)
	card_table.card_select.card_table_card_select_window_confirm.connect(_on_card_table_card_select_window_confirm)


func enter() -> void:
	var release_card_view = battle_state_machine.release_card_view
	release_card_view.visible = false
	release_card_view.reparent(self)
	
	var card_select = card_table.card_select
	card_select.scale = Vector2(0,0)
	card_select.visible = true
	card_select.card_min = release_card_view.card.card_min
	card_select.card_max = release_card_view.card.card_max
	var tween = create_tween();
	tween.tween_property(card_select, "scale", Vector2(1,1), 0.1)
	CardManager.round_data.card_switch_select_mode = true


func _on_card_table_card_select_window_cancel(card_views):
	CardManager.round_data.card_switch_select_mode = false
	for cv in card_views:
		move_card(cv, card_table.card_container)
	var card_view = battle_state_machine.release_card_view
	card_view.visible = true
	move_card(card_view, card_table.card_container)
	card_table.card_select.clear_card()
	transition_requested.emit(self, State.STATE_IDLE)
	
	
	
func _on_card_table_card_select_window_confirm(card_views):
	CardManager.round_data.card_switch_select_mode = false
	await battle_state_machine.release_card_view.card.select_confirm(self)

	for cv in card_views:
		CardManager.discard_pool.add_card(cv.card)
	CardManager.discard_pool.add_card(battle_state_machine.release_card_view.card)
	card_table.card_select.clear_card()
	transition_requested.emit(self, State.STATE_IDLE)
	
	
func move_card(card_view, other_card_container):
	card_view.card_container.card_in_preview = null
	card_view.card_container.card_in_select = null
	card_view.reparent(other_card_container)
	card_view.card_container = other_card_container
	card_view.card_container.do_organize_cards()	
	
